//
//  BaseViewController.swift
//  ONESwift
//
//  Created by wangkun on 2017/6/14.
//  Copyright © 2017年 wangkun. All rights reserved.
//

import UIKit

let application = UIApplication.shared

enum NavBarBtnLoc {
    case left
    case right
}

struct TopStatusInfo {
    var statusBarHide   : Bool = false  //  是否隐藏状态栏
    var statusBarStyle  : UIStatusBarStyle = .lightContent  //  状态栏风格
    var navBarHide      : Bool = false  //  是否隐藏NavBar
    var showNavBarLine  : Bool = true  //  是否显示NavBar底部线条
}

open class BaseViewController: UIViewController {
    
    /// 顶部各元素状态
    var topStatus = TopStatusInfo()
    /// 返回按钮
    var btnBack = UIButton(type: .custom)
    
    /// 是否第一次显示当前页面
    var isFirst = true
    
    override open func viewDidLoad() {
        super.viewDidLoad()
        self.initKeyBoard()
        btnBack.titleLabel?.font = UIFont.systemFont(ofSize: 14)
    }
    
    override open func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        self.refreshTopStatus()
    }
    
    override open func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        application.setStatusBarHidden(topStatus.statusBarHide, with: .slide)
    }
    
    func refreshTopStatus() {
        application.setStatusBarStyle(topStatus.statusBarStyle, animated: true)
        
        if self.parent == nil || !(self.parent?.isKind(of: UIPageViewController.classForCoder()))! {
            self.navigationController?.navigationBar.showLine(show: topStatus.showNavBarLine)
            self.navigationController?.setNavigationBarHidden(topStatus.navBarHide, animated: true)
        }
    }
    
    //MARK: NavBar按钮相关处理
    /// 显示左边的返回按钮
    ///
    /// - Parameter image: 按钮图片
    func showBackButton(image: UIImage? = nil) {
        if image != nil {
            btnBack.setImage(image, for: .normal)
        } else {
            btnBack.setTitle("返回", for: .normal)
        }
        
        btnBack.setTitleColor(UINavigationBar.appearance().tintColor, for: .normal)
        btnBack.addTarget(self, action: #selector(backButtonClick), for: .touchUpInside)
        btnBack.sizeToFit()
        setNavButton(loc: .left, btns: [btnBack])
    }
    
    /// 设置navbar按钮
    ///
    /// - Parameters:
    ///   - loc:  位置
    ///   - btns: 按钮
    func setNavButton(loc: NavBarBtnLoc, btns: Array<UIButton>) {
        let negativeSpacer = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        negativeSpacer.width = -10
        var array = Array<UIBarButtonItem>()
        array.append(negativeSpacer)
        for button in btns {
            let barBtnItem = UIBarButtonItem.init(customView: button)
            if loc == .left {
                array.append(barBtnItem)
            } else {
                array.insert(barBtnItem, at: 1)
            }
        }
        print(self.navigationItem)
        if loc == .left {
            self.navigationItem.leftBarButtonItems = array
        } else {
            self.navigationItem.rightBarButtonItems = array
        }
    }
    
    /// 增加navigationBar按钮
    ///
    /// - Parameters:
    ///   - loc:  位置
    ///   - btns: 按钮
    func addNavButton(loc: NavBarBtnLoc, btns: Array<UIButton>) {
        let negativeSpacer = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        negativeSpacer.width = -10
        var array = Array<UIBarButtonItem>()
        array.append(negativeSpacer)
        for button in btns {
            let barBtnItem = UIBarButtonItem.init(customView: button)
            if loc == .left {
                array.append(barBtnItem)
            } else {
                array.insert(barBtnItem, at: 1)
            }
        }
        print(self.navigationItem)
        if loc == .left {
            self.navigationItem.leftBarButtonItems?.append(contentsOf: array)
        } else {
            self.navigationItem.rightBarButtonItems?.append(contentsOf: array)
        }
    }
    
    /// 返回按钮点击事件
    @objc func backButtonClick() {
        if self.navigationController != nil {
            self.navigationController?.popViewController(animated: true)
        } else {
            self.dismiss(animated: true, completion: nil)
        }
    }
    
    override open func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    deinit {
        removeAllN(self)
    }

}

//MARK: 键盘相关监控方法
extension BaseViewController {
    
    fileprivate func initKeyBoard() {
        addN(self, #selector(keyBoardWillShow(_:)), NSNotification.Name.UIKeyboardWillShow)
        addN(self, #selector(keyBoardWillHide(_:)), NSNotification.Name.UIKeyboardWillHide)
    }
    
    @objc fileprivate func keyBoardWillShow(_ notification: Notification){
        print(notification)
        //获取userInfo
        let info = notification.userInfo
        //获取键盘的size
        let value = info?[UIKeyboardFrameEndUserInfoKey] as? NSValue
        if value != nil {
            let rect = value?.cgRectValue
            if rect != nil {
                self.keyBoardHeight(height: (rect?.size.height)!)
            }
        }
    }
    
    @objc fileprivate func keyBoardWillHide(_ notification: Notification){
        self.keyBoardHidden()
    }
    
    open func keyBoardHeight(height: CGFloat) {}
    open func keyBoardHidden() {}
}
